@{
    ViewData["Title"] = "Address book";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<section id="page-title">
    <h1 class="title pull-left">
        <div class="page-icon">
            <i class="fa fa-users"></i>
        </div> 字典管理
    </h1>
    <ul class="breadcrumb pull-left">
        <li><a href="/User"><i class="fa fa-home"></i> 首页</a></li>
        <li class="active">字典</li>
    </ul>
    <div class="clearfix"></div>
</section>
<section id="page-content">
    <!-- Ajax Error -->
    <div class="alert alert-danger ajax-error" style="display:none">
        <p><strong>错误</strong></p>
        <span class="ajax-error-message"></span>
    </div>
    <div class="row">
        <div class="col-md-2">

            <div class="west-Panel">
                <div class="panel-Title">
                    目录信息<div class="btn-group margin-r-sm">
                        <button type="button" class="btn btn-action btn-xs dropdown-toggle" data-toggle="dropdown">
                            <i class="fa fa-plus-circle"></i> 操作 <span class="fa fa-caret-down"></span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            <li>
                                <a id="lbAddGroupRoot" onclick="btn_datacategory()">分类管理</a>
                            </li>
                        </ul>

                    </div>
                </div>

                <div id="itemTree"></div>
            </div>
        </div>
        <div class="col-md-10">
            <div id="zone-feature" class="zone-instance can-configure">
                <div class="zone-content">
                    <div class="block-content">
                        <div class="panel panel-block">
                            <div class="panel-heading">
                                <div class="pull-left">
                                    <table>
                                        <tr>
                                            <td>
                                                <div id="queryCondition" class="btn-group">
                                                    <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                                    <ul class="dropdown-menu">
                                                        <li><a data-value="ItemName">项目名</a></li>
                                                        <li><a data-value="ItemValue">项目值</a></li>
                                                        <li><a data-value="SimpleSpelling">拼音</a></li>
                                                    </ul>
                                                </div>
                                            </td>
                                            <td style="padding-left: 2px;">
                                                <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                                            </td>
                                            <td style="padding-left: 5px;">
                                                <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="grid-actions pull-right">
                                    <a title="刷新" class="btn btn-default" onclick="SF.utility.reload();"><i class="fa fa-refresh"></i></a>
                                    <a title="新增" class="btn-add btn btn-default btn-sm" onclick="btn_add()"><i class="fa fa-plus-circle"></i></a>
                                    <a title="编辑" class="btn-add btn btn-default btn-sm" onclick="btn_edit()"><i class="fa  fa-edit"></i></a>
                                    <a title="删除" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i></a>
                                    <a title="详细" class="btn btn-default" onclick="btn_detail()"><i class="fa fa-list-alt"></i></a>
                                </div>
                            </div>
                            <div class="panel-body">
                                <div class="grid grid-panel">
                                    @*<div class="grid-filter">
                                            <header>
                                                <h3>
                                                    筛选选择
                                                </h3>
                                                <i class="fa fa-chevron-down toggle-filter"></i>
                                            </header>
                                            <div class="grid-filter-overview">
                                            </div>
                                            <div class="grid-filter-entry" style="display:none;">
                                                <fieldset>
                                                    <h4>筛选选择</h4>
                                                    <div class="row">
                                                        <div class="col-lg-4">
                                                            <div class="form-group sf-text-box ">
                                                                <label class="control-label" for="Account">账户</label>
                                                                <div class="control-wrapper">
                                                                    <input name="Account" type="text" id="Account" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-lg-4">
                                                            <div class="form-group sf-text-box ">
                                                                <label class="control-label" for="RealName">姓名</label>
                                                                <div class="control-wrapper">
                                                                    <input name="RealName" type="text" id="RealName" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-lg-4">
                                                            <div class="form-group sf-text-box ">
                                                                <label class="control-label" for="Mobile">手机</label>
                                                                <div class="control-wrapper">
                                                                    <input name="Mobile" type="text" id="Mobile" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </fieldset>
                                                <a id="btn_Search" title="Apply Filter" class="filter btn btn-action btn-xs">查询</a>
                                            </div>
                                        </div>*@
                                    <div class="table-responsive">
                                        <div class="gridPanel">
                                            <table id="gridTable"></table>
                                            <div id="gridPager"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    //加载树
    var _itemId = "";
    var _itemName = "";
    var _isTree = "";

    $(function () {

        InitialPage();
        GetGrid();
        GetTree();

    });


    //初始化页面
    function InitialPage() {
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 200.5);
            }, 200);
            e.stopPropagation();
        });
    }
    function GetTree() {
        var item = {
            height: $(window).height() - 52,
            url: "/Api/DataItem/GetTreeJson",
            onnodeclick: function (item) {
                _itemId = item.id;
                //展开下级
                $(".bbit-tree-selected").children('.bbit-tree-ec-icon').trigger("click");
                $('#btn_Search').trigger("click");
            },
        };
        //初始化
        $("#itemTree").treeview(item);
    }


    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 200.5,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '&nbsp;&nbsp;&nbsp;&nbsp;项目名', name: 'ItemName', index: 'ItemName', width: 200, align: 'left', sortable: false },
                { label: '项目值', name: 'ItemValue', index: 'ItemValue', width: 200, align: 'left', sortable: false },
                { label: '简拼', name: 'SimpleSpelling', index: 'SimpleSpelling', width: 150, align: 'left', sortable: false },
                { label: '排序', name: 'SortCode', index: 'SortCode', width: 80, align: 'center', sortable: false },
                {
                    label: "默认", name: "IsDefault", index: "IsDefault", width: 50, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    }
                },
                {
                    label: "有效", name: "EnabledMark", index: "EnabledMark", width: 50, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    }
                },
                { label: "备注", name: "Description", index: "Description", width: 200, align: "left", sortable: false }
            ],
            treeGrid: true,
            treeGridModel: "nested",
            ExpandColumn: "ItemValue",
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            $gridTable.jqGrid('setGridParam', {
                url: "/Api/DataItemDetail/GetPageList",
                postData: {
                    itemId: _itemId,
                    condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                    keyword: $("#txt_Keyword").val()
                },
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }
    //字典分类
    function btn_datacategory() {
        //新增
        SF.utility.dialogOpen({
            id: "Form",
            title: '字典分类',
            url: '/DataItem/List',
            width: "800px",
            height: "500px",
            callBack: function (iframeId) {
                LoadTree();
            }
        });

    }

    //新增
    function btn_add() {
        if (!_itemId) {
            return false;
        }
        var parentId = $("#gridTable").jqGridRowValue("Id");
        if (_isTree != 1) {
            parentId = 0;
        }
        SF.utility.dialogOpen({
            id: "Form",
            title: '添加字典',
            url: '/DataItem/ValueForm?parentId=' + parentId + "&itemId=" + _itemId,
            width: "800px",
            height: "500px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick("POST");
            }
        });
    };
    //编辑
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (SF.utility.checkedRow(keyValue)) {
            SF.utility.dialogOpen({
                id: "Form",
                title: '编辑字典',
                url: '/DataItem/ValueForm?keyValue=' + keyValue,
                width: "800px",
                height: "500px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick("PUT");
                }
            });
        }
    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            SF.utility.removeForm({
                type: "DELETE",
                url: "/Api/DataItemDetail/" + keyValue,
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            SF.utility.dialogMsg('请选择需要删除的字典！', 0);
        }
    }
    //详细
    function btn_detail() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (SF.utility.checkedRow(keyValue)) {
            SF.utility.dialogOpen({
                id: "Detail",
                title: '字典信息',
                url: '/DataItem/ValueDetailForm?keyValue=' + keyValue,
                width: "800px",
                height: "500px",
                btn: null
            });
        }
    }
</script>